using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;
using DotNetNuke.Common;

namespace PartyBoy.TweetMode.Components
{
    /// <summary>
    /// Provides strong typed access to settings used by module
    /// </summary>
    public class TweetModeSettings
    {
        ModuleController controller;
        int tabModuleId;

        public TweetModeSettings(int tabModuleId)
        {
            controller = new ModuleController();
            this.tabModuleId = tabModuleId;
        }

        protected T ReadSetting<T>(string settingName, T defaultValue)
        {
            Hashtable settings = controller.GetTabModuleSettings(this.tabModuleId);

            T ret = default(T);

            if (settings.ContainsKey(settingName))
            {
                System.ComponentModel.TypeConverter tc = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T));
                try
                {
                    ret = (T)tc.ConvertFrom(settings[settingName]);
                }
                catch
                {
                    ret = defaultValue;
                }
            }
            else
                ret = defaultValue;

            return ret;
        }

        protected void WriteSetting(string settingName, string value)
        {
            controller.UpdateTabModuleSetting(this.tabModuleId, settingName, value);
        }

        #region public properties
        public string ConsumerKey
        {
            get { return ReadSetting<string>("ConsumerKey", string.Empty); }
            set { WriteSetting("ConsumerKey", value); }
        }
        public string ConsumerSecret
        {
            get { return ReadSetting<string>("ConsumerSecret", string.Empty); }
            set { WriteSetting("ConsumerSecret", value); }
        }
        public string CallbackUrl
        {
            get { return ReadSetting<string>("CallbackUrl", string.Empty); }
            set { WriteSetting("CallbackUrl", value); }
        }
        public int Count
        {
            get { return ReadSetting<int>("Count", 0); }
            set { WriteSetting("Count", value.ToString()); }
        }
        #endregion
    }
}
